using System.ComponentModel.Composition;
using System.Windows.Input;
using VisualWip.Core.Columns;

namespace VisualWip.PresentationModel
{
    public abstract class WorkItemProviderViewModelBase : ColumnProviderViewModelBase
    {
        private ICommand _addDoingColumnCommand;
        private ICommand _addDoingDoneGroupCommand;
        private ICommand _addDoneColumnCommand;
        private ICommand _addSingleColumnCommand;
        private ICommand _addSingleWorkItemColumnCommand;
        private ICommand _addWorkInProgressGroupCommand;

        protected WorkItemProviderViewModelBase(IColumnBuilder columnBuilder):base(columnBuilder)
        {
        }

        public ICommand AddSingleColumnCommand
        {
            get
            {
                if (_addSingleColumnCommand == null)
                {
                    _addSingleColumnCommand = new RelayCommand(param => AddSingleColumn());
                }
                return _addSingleColumnCommand;
            }
        }

        public ICommand AddDoneColumnCommand
        {
            get
            {
                if (_addDoneColumnCommand == null)
                {
                    _addDoneColumnCommand = new RelayCommand(param => AddDoneColumn());
                }
                return _addDoneColumnCommand;
            }
        }

        public ICommand AddDoingColumnCommand
        {
            get
            {
                if (_addDoingColumnCommand == null)
                {
                    _addDoingColumnCommand = new RelayCommand(param => AddDoingColumn());
                }
                return _addDoingColumnCommand;
            }
        }

        public ICommand AddDoingDoneGroupCommand
        {
            get
            {
                if (_addDoingDoneGroupCommand == null)
                {
                    _addDoingDoneGroupCommand = new RelayCommand(param => AddDoingDoneGroup());
                }
                return _addDoingDoneGroupCommand;
            }
        }

        public ICommand AddWorkInProgressGroupCommand
        {
            get
            {
                if (_addWorkInProgressGroupCommand == null)
                {
                    _addWorkInProgressGroupCommand = new RelayCommand(param => AddWorkInProgressGroupColumn());
                }
                return _addWorkInProgressGroupCommand;
            }
        }

        public ICommand AddSingleWorkItemColumnCommand
        {
            get
            {
                if (_addSingleWorkItemColumnCommand == null)
                {
                    _addSingleWorkItemColumnCommand = new RelayCommand(param => AddSingleWorkItemColumn());
                }
                return _addSingleWorkItemColumnCommand;
            }
        }

        #region IColumnProviderViewModel Members

        #endregion

        private void AddDoingDoneGroup()
        {
            var settings = ColumnBuilder.CreateWorkItemProviderColumn<WorkDoingDoneColumn>(ProviderName);
            ISettingsWindow dialog = ColumnBuilder.OpenSettings(settings);

            if (dialog.DialogResult.HasValue && dialog.DialogResult == true)
            {
                WorkDoingColumn doing = settings.GetWorkItemDoingColumnSetting();
                WorkDoneColumn done = settings.GetWorkItemDoneColumnSetting();
                WorkInProgressGroupColumn group = settings.GetWorkInProgressGroupSettings();

                group.Width = doing.Width + done.Width;

                ColumnBuilder.AddColumn(group, true);

                doing.PositionX = group.PositionX;
                doing.PositionY = group.PositionY + 50;
                doing.Height = group.Height - 50;

                done.PositionX = group.PositionX + doing.Width;
                done.PositionY = group.PositionY + 50;
                done.Height = group.Height - 50;

                ColumnBuilder.AddColumn(doing, false);
                ColumnBuilder.AddColumn(done, false);
            }
        }

        private void AddDoneColumn()
        {
            ColumnBuilder.BuildWorkItemProviderColumn<WorkDoneColumn>(ProviderName);
        }

        private void AddDoingColumn()
        {
            ColumnBuilder.BuildWorkItemProviderColumn<WorkDoingColumn>(ProviderName);
        }

        private void AddSingleColumn()
        {
            ColumnBuilder.BuildWorkItemProviderColumn<WorkSingleColumn>(ProviderName);
        }

        private void AddSingleWorkItemColumn()
        {
            ColumnBuilder.BuildWorkItemProviderColumn<SingleWorkItemColumn>(ProviderName);
        }

        private void AddWorkInProgressGroupColumn()
        {
            ColumnBuilder.BuildWorkItemProviderColumn<WorkInProgressGroupColumn>(ProviderName);
        }
    }
}